<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'del_brand':
      del_brand($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function del_brand($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  //find userfile's id and path
  $cond_vals = new stdClass();
  $cond_vals->c = 't.id = :v1';
  $cond_vals->v = array(':v1' => $q);
  $item = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_brand', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d[0];
  //delete path
  $item->file = iconv('UTF-8', 'GB2312', $item->file);
  if (file_exists($item->file)) {
    @unlink($item->file);
  }
  //delete brand
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $item->id);
  DbUtils::deleteByCond($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_brand', $cond_vals);
  
  echo json_encode(array());
}

?>